﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;

namespace PrisonStep
{
    public class PieBazooka
    {
        #region Feilds

            /// <summary>
            /// Game that uses the bazooka
            /// </summary>
            private PrisonGame game;

            private Effect effect;

            public int section = 1;
            public Vector3 FourthColor = Vector3.Zero;
            public Vector3 FourthLocation = Vector3.Zero;
            public float FourthIntensity = 0;

            /// <summary>
            /// The model we loaded
            /// </summary>
            private Model model;

            /// <summary>
            /// Bazooka location
            /// </summary>
            private Vector3 location;

            /// <summary>
            /// Bazooka orientation
            /// </summary>
            private Vector3 orientation = new Vector3(MathHelper.ToRadians(109.5f), MathHelper.ToRadians(9.7f), MathHelper.ToRadians(72.9f));

            /// <summary>
            /// The number of pies left to fire in the bazooka
            /// </summary>
            private int totalPies;

            /// <summary>
            /// The current transform
            /// </summary>
            private Matrix transform;

        //rotate and translation values based on 0,0,0 relative to Victoria's hands:
        //rotation (110, -288, 63)
        //Translation (-8.1, 137.9, 24.4)

        #endregion

        #region Properties

            /// <summary>
            /// The number of pies left to fire in the bazooka
            /// </summary>
            public int TotalPies { get { return totalPies; } set { totalPies = value; } }

            /// <summary>
            /// The current bazooka transformation
            /// </summary>
            public Matrix Transform { get { return transform; } set { transform = value; } }

            /// <summary>
            /// The current bazooka position
            /// </summary>
            public Vector3 Location { get { return location; } set { location = value + new Vector3(-8.1f, 137.9f, 24.4f); } }

            /// <summary>
            /// The current bazooka orientation
            /// </summary>
            public Vector3 Orientation { get { return orientation; } set { orientation = value; } }
        #endregion

        public PieBazooka(PrisonGame game)
        {
            this.game = game;
        }

        public void LoadContent(ContentManager content)
        {
            effect = content.Load<Effect>("BasicModel");
            model = content.Load<Model>("PieBazooka");
            SetEffect();
        }

        public void Update(GameTime gametime)
        {
            //location = transform.Translation;
            //orientation = transform.Backward;
        }

        public void Draw(GraphicsDeviceManager graphics, GameTime gameTime, Matrix transform)
        {
            DrawModel(graphics, model, transform);
        }

        /// <summary>
        /// This function is called to draw this game component.
        /// </summary>
        /// <param name="graphics"></param>
        /// <param name="gameTime"></param>
        public void Draw(GraphicsDeviceManager graphics, GameTime gameTime)
        {
            DrawModel(graphics, model, Transform);
        }

        private void DrawModel(GraphicsDeviceManager graphics, Model model, Matrix world)
        {

            Matrix[] transforms = new Matrix[model.Bones.Count];
            model.CopyAbsoluteBoneTransformsTo(transforms);

            foreach (ModelMesh mesh in model.Meshes)
            {
                foreach (Effect effect in mesh.Effects)
                {
                    effect.Parameters["World"].SetValue(transforms[mesh.ParentBone.Index] * world);
                    effect.Parameters["View"].SetValue(game.Camera.View);
                    effect.Parameters["Projection"].SetValue(game.Camera.Projection);
                    effect.Parameters["Light1Location"].SetValue(game.LightInfo(section, 0));
                    effect.Parameters["Light1Color"].SetValue(game.LightInfo(section, 1));
                    effect.Parameters["Light2Location"].SetValue(game.LightInfo(section, 2));
                    effect.Parameters["Light2Color"].SetValue(game.LightInfo(section, 3));
                    effect.Parameters["Light3Location"].SetValue(game.LightInfo(section, 4));
                    effect.Parameters["Light3Color"].SetValue(game.LightInfo(section, 5));
                    effect.Parameters["Light4Location"].SetValue(this.FourthLocation);
                    effect.Parameters["Light4Color"].SetValue(this.FourthColor * FourthIntensity);
                    effect.Parameters["Greeness"].SetValue(game.Greeness);
                }
                mesh.Draw();
            }
        }

        private void SetEffect()
        {
            foreach (ModelMesh mesh in model.Meshes)
            {
                foreach (ModelMeshPart part in mesh.MeshParts)
                {
                    BasicEffect bEffect = part.Effect as BasicEffect;
                    part.Effect = effect.Clone();
                }
            }
        }

        
    }
}
